package main

import (
	"context"
	"git.900sui.cn/kc/base/utils"
	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/kcgin/logs"
	"git.900sui.cn/kc/rabbitmq"
	"git.900sui.cn/kc/rpcinterface/client/bus"
	busEs "git.900sui.cn/kc/rpcinterface/client/elastic/bus"
	bus2 "git.900sui.cn/kc/rpcinterface/interface/bus"

	"github.com/streadway/amqp"
	"strconv"
)

// 根据卡包信息添加/更新会员持卡量
// @author lixiaojun
func main() {
	logs.Info("Environment Variable:MSF_ENV:", kcgin.KcConfig.RunMode)
	new(utils.KcLog).GetInstance(kcgin.AppConfig.String("queue.log.addMemberCardNum"))
	err := rabbitmq.Rabbitmq.Consume(kcgin.AppConfig.String("queue.addMemberCardNum"), func(msg amqp.Delivery) {
		relationId, _ := strconv.Atoi(string(msg.Body))
		rpcBusMember := new(bus.Member).Init()
		var reply bus2.ReplyAddMemberConsumeCount
		if err := rpcBusMember.AddMemberCardNum(context.Background(), &relationId, &reply); err != nil {
			utils.Logerr.Printf("根据卡包信息添加/更新会员持卡量处理任务调用失败，relationId:%v, err:%v", relationId, err)
		}
		memberId := reply.MemberId
		if memberId > 0 {
			//将数据同步更新到es
			rpcBusMemberEs := new(busEs.Member).Init()
			defer rpcBusMemberEs.Close()
			var rpcBusMemberEsRes bool
			if err := rpcBusMemberEs.SetMemberConsumeCount(context.Background(), &memberId, &rpcBusMemberEsRes); err != nil {
				utils.Logerr.Printf("根据卡包信息添加/更新会员持卡量更新到ES 任务执行失败，relationId:%d ,memberId:%d, err:%s", relationId, memberId, err)
			}
			utils.Logerr.Printf("根据卡包信息添加/更新会员持卡量更新到ES 任务执行成功，relationId:%d ,memberId:%d", relationId, memberId)
		}
		utils.Logerr.Printf("根据卡包信息添加/更新会员持卡量处理任务调用成功，relationId:%v", relationId)
	})
	if err != nil {
		utils.Logerr.Printf("根据卡包信息添加/更新会员持卡量处理任务错误，err: %s", err.Error())
	}
}
